import { _decorator, Component, Node, Vec3 } from 'cc';
import { GlobalEnum } from '../../../Init/Config/GlobalEnum';
import { EventTypes } from '../../../Init/Managers/EventTypes';
import GlobalPool from '../../../Init/Tools/GlobalPool';
import { BasicLayer } from '../Common/Basic/BasicLayer';
import { BasicEffect } from './BasicEffect';
import { BoomArea } from './BoomArea';
import { GuideArrow } from './GuideArrow';
import { MappingMine } from './MappingMine';
import { MappingMissile } from './MappingMissile';
import { MappingObj } from './MappingObj';
const { ccclass, property } = _decorator;

@ccclass('EffectLayer')
export class EffectLayer extends BasicLayer {

    protected _effectRecs: { [uuid: string]: BasicEffect } = {};
    // #region -------------------------------层级生命周期------------
    /**初始化 只执行一次*/
    protected init() {

    };
    /**注册通过自定义事件管理器管理的事件  */
    protected onEvents() {
        this.on(EventTypes.EffectEvents.ShowBoomErea, this.onShowBoomArea, this);
        this.on(EventTypes.EffectEvents.showGuideArrow, this.onShowGuideArrow, this);
        this.on(EventTypes.Mapping3DEvents.SetMissiles, this.onSetMissiles, this);
        this.on(EventTypes.Mapping3DEvents.SetProps, this.onSetProps, this);
        this.on(EventTypes.EffectEvents.showGold3d, this.onShowGold3d, this);
    };
    /**设置状态、数据等, */
    public setData(data?: any) {

    };
    /**重置状态、数据等，子类实现 ,注销事件*/
    public reset() {
        this._effectRecs = {};

    }

    public customUpdate(dt: number) {
        for (const key in this._effectRecs) {
            const e = this._effectRecs[key];
            if (e && e.node.active) {
                e.update(dt);
            } else {
                e && e.reset();
                delete this._effectRecs[key];
            }
        }
        this.updateMappingEffect(dt);
    }
    public customLateUpdate(dt: number) {

    }
    // #endregion

    //#region ----------------------爆炸区域---------------
    protected onShowBoomArea(d: { p: Vec3, s: number, t: number }) {
        let e = GlobalPool.get('boomArea');
        let cmp = new BoomArea(e);
        cmp.init(this.node, d.p, d.s, d.t);
        this._effectRecs[e.uuid] = cmp;
    }
    //#endregion

    //#region ----------------------映射3d模型--------------
    protected _mappingRecs: { [uuid: string]: MappingObj } = {};
    protected resetMappingEffect() {
        this._mappingRecs = {};
    }
    protected updateMappingEffect(dt) {
        for (const key in this._mappingRecs) {
            const e = this._mappingRecs[key];
            if (e && e.node.active) {
                e.update(dt);
            } else {
                delete this._mappingRecs[key];
            }
        }
    }

    /**导弹3d */
    protected onSetMissiles(d: { uuid: string, p: Vec3, toP: Vec3, t: number }) {
        let e = GlobalPool.get('missile3d');
        e.parent = this.node;
        let cmp = new MappingMissile(e);
        cmp.init(d);
        this._mappingRecs[e.uuid] = cmp;
    }

    /**道具 */
    protected onSetProps(d: { type: GlobalEnum.PropsType, node: Node, state: number, data?}[]) {
        for (const key in d) {
            const rec = d[key];
            let node: Node, cmp: MappingObj;
            switch (rec.type) {
                case GlobalEnum.PropsType.Mine:
                    node = GlobalPool.get('mine3d');
                    node.parent = this.node;
                    cmp = new MappingMine(node, rec);
                    break;

                default:
                    break;
            }
            cmp.init();
            this._mappingRecs[node.uuid] = cmp;
        }
    }

    /**指示箭头 */
    protected onShowGuideArrow(followNode) {
        let e = GlobalPool.get('guideArrow');
        e.parent = this.node;
        let cmp = new GuideArrow(e);
        cmp.init(followNode);
        this._effectRecs[e.uuid] = cmp;
    }
    //3d金币
    protected onShowGold3d(p) {
        let e = GlobalPool.get('Gold3d', p);
        e.setPosition(p);
        e.eulerAngles = Vec3.ZERO;
        e.parent = this.node;
    }

    //#endregion
}

